{% set pypi_name = 'monasca-persister' %}
{% set source = fetch_source('https://tarballs.openstack.org/monasca-persister/monasca-persister-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

%global sname {{ pypi_name }}
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        Persists state transistions in database
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca
Source0:        {{ source|basename }}
Source1:        openstack-monasca-persister.service
Source2:        openstack-monasca-persister.logrotate
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('cassandra-driver') }}
BuildRequires:  {{ py3('elasticsearch') }}
BuildRequires:  {{ py3('influxdb') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monasca-common') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('setuptools') }}
BuildRequires:  {{ py3('simplejson') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
Requires(pre):  pwdutils
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(pre):  shadow-utils
Requires(preun): systemd
%endif

%description
Consumes metrics and alarm state transitions from message queue and stores them in database.
Data is read from Kafka queue and saved either in InfluxDB or Cassandra

%package -n python3-{{ pypi_name }}
Summary:        Persists state transistions in database
Group:          Development/Languages/Python
Requires:       {{ py3('cassandra-driver') }}
Requires:       {{ py3('influxdb') }}
Requires:       {{ py3('monasca-common') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('simplejson') }}
Requires:       {{ py3('six') }}

%description -n python3-{{ pypi_name }}
Consumes metrics and alarm state transitions from message queue and stores them in database.
Data is read from Kafka queue and saved either in InfluxDB or Cassandra

%prep
%autosetup -n %{sname}-{{ upstream_version }}
%py_req_cleanup

%build
%{py3_build}
PYTHONPATH=. oslo-config-generator \
  --config-file config-generator/persister.conf \
  --output-file etc/monasca/persister.conf.sample

%install
%{py3_install}

# create all necessary directories
install -d -m 755 %{buildroot}%{_sysconfdir}/monasca
install -d -m 755 %{buildroot}%{_var}/lib/{{ pypi_name }}
install -d -m 755 %{buildroot}%{_var}/log/{{ pypi_name }}

# install config
install -D -m 640 etc/monasca/persister-logging.conf %{buildroot}%{_sysconfdir}/monasca/persister-logging.conf
install -D -m 644 etc/monasca/persister.conf.sample %{buildroot}%{_sysconfdir}/monasca/persister.conf

# install systemd service
install -p -D -m 444 %{SOURCE1} %{buildroot}%{_unitdir}/openstack-monasca-persister.service

# install symlinks on SUSE
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
%endif

# Install logrotate
install -p -D -m 644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-monasca-persister.logrotate

%check
python3 -m stestr.cli run

%pre
%openstack_pre_user_group_create monasca-persister monasca /sbin/nologin

%preun
%systemd_preun openstack-monasca-persister.service

%post
%systemd_post openstack-monasca-persister.service


%postun
%systemd_postun_with_restart openstack-monasca-persister.service
if [ $1 -eq 0 ] ; then
  getent passwd monasca-persister > /dev/null
  if [ "$?" == "0" ] ; then
      userdel monasca-persister
  fi
fi

%files
%license LICENSE
%doc README.rst ChangeLog
%dir %attr(-, root, monasca) %{_sysconfdir}/monasca
%dir %attr(0750, monasca-persister, monasca) %{_var}/lib/{{ pypi_name }}
%dir %attr(0750, monasca-persister, monasca) %{_var}/log/{{ pypi_name }}
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/persister.conf
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/persister-logging.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/openstack-monasca-persister.logrotate
%{_unitdir}/openstack-monasca-persister.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}
%endif
%{_bindir}/monasca-persister

%files -n python3-{{ pypi_name }}
%license LICENSE
%{python3_sitelib}/monasca_persister
%{python3_sitelib}/monasca_persister-*.egg-info

%changelog
